<?php

namespace common\modules\ad\models\backend;

use common\widgets\grid\CheckboxColumn;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * PositionSearch represents the model behind the search form about `common\modules\ad\models\backend\Position`.
 */
class PositionSearch extends Position
{

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'ad_width', 'ad_height', 'created_at', 'updated_at', 'created_by', 'updated_by'], 'integer'],
            [['name', 'description', 'template'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/ad', 'Position');
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Position::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if ( ! $this->validate())
        {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id'         => $this->id,
            'ad_width'   => $this->ad_width,
            'ad_height'  => $this->ad_height,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'created_by' => $this->created_by,
            'updated_by' => $this->updated_by,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'template', $this->template]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        return [
            [
                'class' => CheckboxColumn::className(),
            ],

            [
                'attribute' => 'id',
            ],

            'name',

            'ad_width',

            'ad_height',

            [
                'attribute'  => 'created_at',
                'format'     => ['date', 'php:Y-m-d H:i:s'],
                'filter' => false,
            ],

            [
                'attribute'  => 'updated_at',
                'format'     => ['date', 'php:Y-m-d H:i:s'],
                'filter' => false,
            ],

            [
                'class' => 'common\widgets\grid\ActionColumn',
            ],
        ];
    }
}
